package com.shensi.adminCollect.controller.coupon;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.domain.entity.SysDept;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.coupon.*;
import com.shensi.adminCollect.service.coupon.*;
import com.shensi.adminCollect.domain.system.CouponNotOil;
import com.shensi.adminCollect.domain.system.SysOil;
import com.shensi.adminCollect.domain.system.SysStationUser;
import com.shensi.adminCollect.service.system.ISysOilService;
import com.shensi.adminCollect.service.system.ISysStationUserService;
import com.shensi.adminCollect.service.system.impl.SysDeptServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

import java.util.*;

/**
 * 优惠劵发放方式Controller
 * @author qujia
 * @date 2021-08-13
 */
@RestController
@RequestMapping("/coupon/issue")
public class CouponIssueController extends BaseController
{
    @Autowired
    private ICouponIssueService couponIssueService;
    @Autowired
    private ICouponIssueRelationService couponIssueRelationService;
    @Autowired
    private ICouponInfoService  couponInfoService;
    @Autowired
    private SysDeptServiceImpl sysDeptService;

    @Autowired
    private ISysStationUserService sysStationUserService;

    @Autowired
    private ISysOilService oilService;

    @Autowired
    private IIssueStationService iIssueStationService;
    @Autowired
    private IIssueOilService iIssueOilService;


    /**
     * 查询优惠劵发放方式列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CouponIssue couponIssue)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if(!"1".equals(currentUser.getUser().getUserLevel())){
           return getDataTable(new ArrayList<>());
        }else {
            couponIssue.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        }
        //couponIssue.setStationId(currentUser.getUser().getDeptId());
        startPage();
        List<CouponIssue> list = couponIssueService.selectCouponIssueList(couponIssue);
        if(list!=null && list.size()>0){
            for(CouponIssue issue:list){
                //将原有分类转化为集合
                if(issue!=null && issue.getShowId()!=null){
                    issue.setShowIdList(Arrays.asList(issue.getShowId().split(",")));
                }
                if(issue!=null && issue.getMemberLevel()!=null){
                    issue.setMemberLevelList(Arrays.asList(issue.getMemberLevel().split(",")));
                }
                if(issue!=null && issue.getPaymentMethod()!=null){
                    issue.setPaymentMethodList(Arrays.asList(issue.getPaymentMethod().split(",")));
                }
                if(issue!=null && issue.getMemberTags()!=null){
                    issue.setMemberTagsList(Arrays.asList(issue.getMemberTags().split(",")));
                }
//                //将油品的关联信息查询出来
//                List<SysOil> oils=new ArrayList<>();
//                IssueOilRelations issueOilRelations=new IssueOilRelations();
//                issueOilRelations.setIssueId(issue.getId());
//                List<IssueOilRelations> issueOilRelations1=iIssueOilService.selectIssueOilRelationsList(issueOilRelations);
//                for(IssueOilRelations issueOilRelations2: issueOilRelations1){
//                    SysOil oil=oilService.selectSysOilById(issueOilRelations2.getOilId());
//                    oils.add(oil);
//                }
//                issue.setOilIdLists(oils);
//
//                //查询关联的油站信息
//                List<SysStationUser> stationUsers=new ArrayList<>();
//                IssueStationRelations issueStationRelations=new IssueStationRelations();
//                issueStationRelations.setIssueId(issue.getId());
//                List<IssueStationRelations> issueStationRelationsList=iIssueStationService.selectIssueStationRelationsList(issueStationRelations);
//                for(IssueStationRelations issueStationRelations1: issueStationRelationsList){
//                    SysStationUser sysStationUser=sysStationUserService.selectSysStationUserById(issueStationRelations1.getStationId());
//                    stationUsers.add(sysStationUser);
//                }
//                issue.setStationLists(stationUsers);
//                //获取关联的优惠卷信息
//                List<CouponInfo> couponInfoList=new ArrayList<>();
//                CouponIssueRelation couponIssueRelation2=new CouponIssueRelation();
//                couponIssueRelation2.setIssueId(issue.getId());
//                List<CouponIssueRelation> couponIssueRelationList=couponIssueRelationService.selectCouponIssueRelationList(couponIssueRelation2);
//                for(CouponIssueRelation couponIssueRelation1: couponIssueRelationList){
//                    CouponInfo couponInfo=couponInfoService.selectCouponInfoById(Integer.parseInt(couponIssueRelation1.getCouponId()+""));
//                    couponInfoList.add(couponInfo);
//                }
//                issue.setCouponList(couponInfoList);
//
//                CouponIssueRelation couponIssueRelation =new CouponIssueRelation();
//                couponIssueRelation.setIssueId(issue.getId());
//                //添加发放方式的信息和优惠卷关联表
//                List<CouponIssueRelation> detailList = couponIssueRelationService.selectCouponIssueRelationList(couponIssueRelation);
//                List<Integer> idlist = couponIssueRelationService.selectCouponIdList(couponIssueRelation);
//                List<CouponInfo> couponInfos=new ArrayList<>();
//                if(detailList!=null &&detailList.size()>0){
//                   for(CouponIssueRelation data:detailList){
//                       couponInfos.add(couponInfoService.selectCouponInfoById(Integer.valueOf(data.getCouponId()+"")));
//                   }
//                   issue.setCouponList(couponInfos);
//
//                    issue.setCouponIssueRelationList(idlist);
//                }
//                CouponIssueRelation couponIssueRelation1=new CouponIssueRelation();
//                couponIssueRelation1.setIssueId(issue.getId());
//                List<CouponIssueRelation>  couponIssueRelations=couponIssueRelationService.selectCouponInfos(couponIssueRelation1);
//                //根据状态来拆分对应的优惠卷的信息，赋值到对应的阶梯直降
//                if("ladder".equals(issue.getThresholdType())) {
//                    List<Threshold> thresholds=new ArrayList<>();
//                    if (couponIssueRelations != null && couponIssueRelations.size() > 0) {
//                        for (CouponIssueRelation couponIssueRelation3 : couponIssueRelations) {
//                         List<String> couponids=Arrays.asList(couponIssueRelation3.getInfos().split(","));
//                         if(couponids!=null&&couponids.size()>0) {
//                             //couponids.remove(couponids.size() - 1);
//                             Threshold threshold = new Threshold();
//                             threshold.setCouponList(couponids);
//                             threshold.setValue(couponIssueRelation3.getThresholdAmt());
//                             thresholds.add(threshold);
//                         }
//                        }
//                    }
//                    issue.setThresholdLadder(thresholds);
//                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询发布过的超级会员关联的发放方式
     */
    @GetMapping("/list4Relation")
    public TableDataInfo list4Relation(CouponIssue couponIssue)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if(!"1".equals(currentUser.getUser().getUserLevel())){
            return getDataTable(new ArrayList<>());
        }else {
            couponIssue.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        }

        startPage();
        couponIssue.setStatus("1");
//        couponIssue.setMemberCondition("plus");
        couponIssue.setIssueType("4");
        List<CouponIssue> list = couponIssueService.selectCouponIssueList4Relation(couponIssue);
        if(list!=null && list.size()>0){
            for(CouponIssue issue:list){
                //将原有分类转化为集合
                if(issue!=null && issue.getShowId()!=null){
                    issue.setShowIdList(Arrays.asList(issue.getShowId().split(",")));
                }
                if(issue!=null && issue.getMemberLevel()!=null){
                    issue.setMemberLevelList(Arrays.asList(issue.getMemberLevel().split(",")));
                }
                if(issue!=null && issue.getPaymentMethod()!=null){
                    issue.setPaymentMethodList(Arrays.asList(issue.getPaymentMethod().split(",")));
                }
                if(issue!=null && issue.getMemberTags()!=null){
                    issue.setMemberTagsList(Arrays.asList(issue.getMemberTags().split(",")));
                }
            }
        }
        return getDataTable(list);
    }


    /**
     * 查询当前人员的发放方式
     */
    @GetMapping("/listByLogin")
    public TableDataInfo listByLogin()
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        List<CouponIssue> list=new ArrayList<CouponIssue>();
        if(currentUser.getUser().getDept().getParentId()!=100&&currentUser.getUser().getDept().getParentId()!=0){
            SysDept sysDept=sysDeptService.selectDeptById(currentUser.getUser().getDept().getParentId());
            if("1".equals(sysDept.getCouponFlag())){
                CouponIssue couponIssue=new CouponIssue();
                couponIssue.setIssueType("4");

                list=couponIssueService.selectCouponIssueList(couponIssue);
            }else {
                CouponIssue couponIssue=new CouponIssue();
                couponIssue.setIssueType("4");

                list=couponIssueService.selectCouponIssueList(couponIssue);
            }
        }
        return getDataTable(list);
    }


    /**
     * 导出优惠劵发放方式列表
     */
    @GetMapping("/export")
    public AjaxResult export(CouponIssue couponIssue)
    {
        List<CouponIssue> list = couponIssueService.selectCouponIssueList(couponIssue);
        ExcelUtil<CouponIssue> util = new ExcelUtil<CouponIssue>(CouponIssue.class);
        return util.exportExcel(list, "issue");
    }

    /**
     * 获取优惠劵发放方式详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        //查询优惠劵发放方式
        CouponIssue couponIssue = couponIssueService.selectCouponIssueById(id);
        if(couponIssue==null){
            return AjaxResult.error("未查询到对应的优惠卷！");
        }
            //将原有分类转化为集合
            if(couponIssue!=null && couponIssue.getShowId()!=null){
                couponIssue.setShowIdList(Arrays.asList(couponIssue.getShowId().split(",")));
            }
            if(couponIssue!=null && couponIssue.getMemberLevel()!=null){
                couponIssue.setMemberLevelList(Arrays.asList(couponIssue.getMemberLevel().split(",")));
            }
            if(couponIssue!=null && couponIssue.getPaymentMethod()!=null){
                couponIssue.setPaymentMethodList(Arrays.asList(couponIssue.getPaymentMethod().split(",")));
            }
            if(couponIssue!=null && couponIssue.getMemberTags()!=null){
                couponIssue.setMemberTagsList(Arrays.asList(couponIssue.getMemberTags().split(",")));
            }
            //将油品的关联信息查询出来
            List<SysOil> oils=new ArrayList<>();
        List<SysOil> notOils = new ArrayList<>();
        List<CouponNotOil> couponNotOils = new ArrayList<>();
            IssueOilRelations issueOilRelations=new IssueOilRelations();
            issueOilRelations.setIssueId(couponIssue.getId());
            List<IssueOilRelations> issueOilRelations1=iIssueOilService.selectIssueOilRelationsList(issueOilRelations);
            for(IssueOilRelations issueOilRelations2: issueOilRelations1){
                SysOil oil=oilService.selectSysOilById(issueOilRelations2.getOilId());
                oils.add(oil);
            }
            couponIssue.setOilIdLists(oils);
            List<IssueOilRelations> issueOilRelations2 = iIssueOilService.selectIssueNotOilRelationsList(issueOilRelations);
        for (IssueOilRelations issueOilRelations22 : issueOilRelations2) {
            SysOil oil = new SysOil();
            oil.setOilId(issueOilRelations22.getOilId());
            notOils.add(oil);
        }
        couponIssue.setNotOilIdLists(notOils);
        for(SysOil notOil:notOils  ){
            CouponNotOil couponNotOil = new CouponNotOil();
            couponNotOil.setCommodityId(notOil.getOilId());
            couponNotOil.setCommodityName(notOil.getOilName());
            couponNotOils.add(couponNotOil);
        }
        couponIssue.setCouponNotOils(couponNotOils);
            //查询关联的油站信息
            List<SysStationUser> stationUsers=new ArrayList<>();
            IssueStationRelations issueStationRelations=new IssueStationRelations();
            issueStationRelations.setIssueId(couponIssue.getId());
            List<IssueStationRelations> issueStationRelationsList=iIssueStationService.selectIssueStationRelationsList(issueStationRelations);
            for(IssueStationRelations issueStationRelations1: issueStationRelationsList){
                SysStationUser sysStationUser=sysStationUserService.selectSysStationUserById(issueStationRelations1.getStationId());
                stationUsers.add(sysStationUser);
            }
            couponIssue.setStationLists(stationUsers);
            //获取关联的优惠卷信息
            List<CouponInfo> couponInfoList=new ArrayList<>();
            CouponIssueRelation couponIssueRelation2=new CouponIssueRelation();
            couponIssueRelation2.setIssueId(couponIssue.getId());
            List<CouponIssueRelation> couponIssueRelationList=couponIssueRelationService.selectCouponIssueRelationList(couponIssueRelation2);
            for(CouponIssueRelation couponIssueRelation1: couponIssueRelationList){
                CouponInfo couponInfo=couponInfoService.selectCouponInfoById(Integer.parseInt(couponIssueRelation1.getCouponId()+""));
                couponInfoList.add(couponInfo);
            }
           couponIssue.setCouponList(couponInfoList);

            CouponIssueRelation couponIssueRelation =new CouponIssueRelation();
            couponIssueRelation.setIssueId(couponIssue.getId());
            //添加发放方式的信息和优惠卷关联表
            List<CouponIssueRelation> detailList = couponIssueRelationService.selectCouponIssueRelationList(couponIssueRelation);
            List<Integer> idlist = couponIssueRelationService.selectCouponIdList(couponIssueRelation);
            List<CouponInfo> couponInfos=new ArrayList<>();
                for(int i =0;i<idlist.size()-1;i++){
                    for(int j=idlist.size()-1;j>i;j--){
                        if(idlist.get(i).equals(idlist.get(j)))
                            idlist.remove(j);
                    }
                }
            if(detailList!=null &&detailList.size()>0){
                for(Integer data:idlist){
                    couponInfos.add(couponInfoService.selectCouponInfoById(Integer.valueOf(data)));
                }
                couponIssue.setCouponList(couponInfos);

                couponIssue.setCouponIssueRelationList(idlist);
            }

            CouponIssueRelation couponIssueRelation1=new CouponIssueRelation();
            couponIssueRelation1.setIssueId(couponIssue.getId());
            List<CouponIssueRelation>  couponIssueRelations=couponIssueRelationService.selectCouponInfos(couponIssueRelation1);
            //根据状态来拆分对应的优惠卷的信息，赋值到对应的阶梯直降
            if("ladder".equals(couponIssue.getThresholdType())) {
                List<Threshold> thresholds=new ArrayList<>();
                if (couponIssueRelations != null && couponIssueRelations.size() > 0) {
                    for (CouponIssueRelation couponIssueRelation3 : couponIssueRelations) {
                        List<String> couponids=Arrays.asList(couponIssueRelation3.getInfos().split(","));
                        if(couponids!=null&&couponids.size()>0) {
                            //couponids.remove(couponids.size() - 1);
                            Threshold threshold = new Threshold();
                            threshold.setCouponList(couponids);
                            threshold.setValue(couponIssueRelation3.getThresholdAmt());
                            thresholds.add(threshold);
                        }
                    }
                }
                couponIssue.setThresholdLadder(thresholds);
            }
        return AjaxResult.success(couponIssue);
    }

    /**
     * 新增优惠劵发放方式
     */
    @ApiOperation("优惠劵发放方式")
    @ApiImplicitParam(name = "couponIssue", value = "优惠劵发放对象", dataType = "CouponIssue")
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody CouponIssue couponIssue)
    {

        int i=0;
        Random random = new Random();
        String result = "";
        String result1 = "";
        for (int m = 0; m < 4; m++) {
            result += random.nextInt(10);
        }
        for (int mm = 0; mm < 4; mm++) {
            result1 += random.nextInt(10);
        }
        String TempTime=System.currentTimeMillis()+"";

        String ordNo = ""+result1+TempTime.substring(5)+result;
        couponIssue.setId(ordNo);

        LoginUser currentUser = SecurityUtils.getLoginUser();
        couponIssue.setEnterpriseId(currentUser.getUser().getEnterpriseId());
        couponIssue.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));

        // 判断couponIssue是否为空，并检查showIdList字段的值是否存在并且长度大于0
        if(couponIssue!=null &&couponIssue.getShowIdList()!=null && couponIssue.getShowIdList().size()>0){
            String verification= StringUtils.join( couponIssue.getShowIdList(),",");
            couponIssue.setShowId(verification);
        }

// 判断couponIssue是否为空，并检查memberLevelList字段的值是否存在并且长度大于0
        if(couponIssue!=null && couponIssue.getMemberLevelList()!=null && couponIssue.getMemberLevelList().size()>0){
            couponIssue.setMemberLevel(StringUtils.join( couponIssue.getMemberLevelList(),","));
        }

// 判断couponIssue是否为空，并检查paymentMethodList字段的值是否存在并且长度大于0
        if(couponIssue!=null && couponIssue.getPaymentMethodList()!=null && couponIssue.getPaymentMethodList().size()>0){
            couponIssue.setPaymentMethod(StringUtils.join( couponIssue.getPaymentMethodList(),","));
        }

// 判断couponIssue是否为空，并检查memberTagsList字段的值是否存在并且长度大于0
        if(couponIssue!=null && couponIssue.getMemberTagsList()!=null && couponIssue.getMemberTagsList().size()>0){
            couponIssue.setMemberTags(StringUtils.join( couponIssue.getMemberTagsList(),","));
        }

// 对领取方式为3（支付成功后的展示位）做限制，同一企业只能设置一个
        if(couponIssue.getIssueType().equals("3")){
            CouponIssue couponIssue1=new CouponIssue();
            couponIssue1.setIssueType("3");
            couponIssue1.setStatus("1");
            couponIssue1.setEnterpriseId(currentUser.getUser().getEnterpriseId());
            List<CouponIssue> couponIssues=couponIssueService.selectCouponIssueList(couponIssue1);
            if(couponIssues.size()>0){
                return AjaxResult.error("支付成功后的展示位，只能设置一个！");
            }
        }
// 检查优惠券列表中的优惠券是否已经过期
        List<CouponInfo> couponInfoList = couponIssue.getCouponList();
        if(couponInfoList!=null && couponInfoList.size()>0) {
            for (CouponInfo couponInfo1 : couponInfoList) {
                if("1".equals(couponInfo1.getEffectiveTimeType())){
                     if(couponInfo1.getEffectiveTimeEnd().before(new Date())){
                         return AjaxResult.error("当前优惠卷已经超过有效期");
                     }
                }
            }
        }
        // 判断couponIssue是否为空，并且领取方式不为5
         if(couponIssue!=null&&!"5".equals(couponIssue.getIssueType())){
             // 检查活动的开始时间是否大于结束时间
           if(couponIssue.getStartDate().after(couponIssue.getEndDate())){
               return AjaxResult.error("活动的开始时间不能大于结束时间");
           }
             // 检查结束时间是否小于当前时间
           if(couponIssue.getEndDate().before(new Date())){
               return AjaxResult.error("结束时间不得小于当前时间");
           }
         }

        //couponIssue.setStationId(currentUser.getUser().getDeptId());
        couponIssue.setStatus("1");
        i=couponIssueService.insertCouponIssue(couponIssue);
        // 处理与油品相关的信息
        if(couponIssue!=null && couponIssue.getOilIdLists()!=null && couponIssue.getOilIdLists().size()>0){
            for(SysOil sysOil:couponIssue.getOilIdLists()){
                IssueOilRelations issueOilRelations=new IssueOilRelations();
                issueOilRelations.setIssueId(couponIssue.getId());
                issueOilRelations.setOilId(sysOil.getOilId());
                iIssueOilService.insertIssueOilRelations(issueOilRelations);
            }
        }
        if (couponIssue != null && couponIssue.getCouponNotOils() != null && couponIssue.getCouponNotOils().size() > 0) {
            for (CouponNotOil sysOil : couponIssue.getCouponNotOils()) {
                IssueOilRelations issueOilRelations = new IssueOilRelations();
                issueOilRelations.setIssueId(couponIssue.getId());
                issueOilRelations.setOilId(sysOil.getCommodityId());
                iIssueOilService.insertIssueNotOilRelations(issueOilRelations);
            }
        }
        // 处理与加油站相关的信息
        if(couponIssue!=null && couponIssue.getStationLists()!=null && couponIssue.getStationLists().size()>0){
            for(SysStationUser sysStationUser:couponIssue.getStationLists()){
               IssueStationRelations issueStationRelations=new IssueStationRelations();
                issueStationRelations.setIssueId(couponIssue.getId());
                issueStationRelations.setStationId(sysStationUser.getStationId());
                iIssueStationService.insertIssueStationRelations(issueStationRelations);
            }
        }
//        List<CouponInfo> list = couponIssue.getCouponList();
//        if(list!=null && list.size()>0){
//            for (CouponInfo couponInfo1 : list){
//                CouponIssueRelation issueRetail = new CouponIssueRelation();
//                CouponInfo couponInfo=couponInfoService.selectCouponInfoById(couponInfo1.getId());
//                couponInfo.setEditFlag("1");
//                i=couponInfoService.updateCouponInfo(couponInfo);
//                issueRetail.setIssueId(couponIssue.getId());
//                issueRetail.setCouponId(Long.valueOf(couponInfo1.getId()));
//                issueRetail.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
//                i=  couponIssueRelationService.insertCouponIssueRelation(issueRetail);
//            }
//        }
        if("ladder".equals(couponIssue.getThresholdType())) {
            // 处理阶梯阈值优惠券关联关系
            if (couponIssue.getThresholdLadder() != null && couponIssue.getThresholdLadder().size() > 0) {
                for (Threshold threshold : couponIssue.getThresholdLadder()) {
                    if (threshold.getCouponList() != null && threshold.getCouponList().size() > 0) {
                        for (String id : threshold.getCouponList()) {
                            CouponIssueRelation issueRetail = new CouponIssueRelation();
                            CouponInfo couponInfo = couponInfoService.selectCouponInfoById(Integer.parseInt(id));
                            couponInfo.setEditFlag("1");
                            i = couponInfoService.updateCouponInfo(couponInfo);
                            issueRetail.setIssueId(couponIssue.getId());
                            issueRetail.setCouponId(Long.valueOf(id));
                            issueRetail.setThresholdAmt(threshold.getValue());
                            issueRetail.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
                            i = couponIssueRelationService.insertCouponIssueRelation(issueRetail);
                        }
                    }
                }
            }
        }else {
            // 处理普通阈值优惠券关联关系
            List<CouponInfo> list = couponIssue.getCouponList();
        if(list!=null && list.size()>0){
            for (CouponInfo couponInfo1 : list){
                CouponIssueRelation issueRetail = new CouponIssueRelation();
                CouponInfo couponInfo=couponInfoService.selectCouponInfoById(couponInfo1.getId());
                couponInfo.setEditFlag("1");
                couponInfo.setMinUseCouponAmt(couponInfo1.getMinUseCouponAmt());
                i=couponInfoService.updateCouponInfo(couponInfo);
                issueRetail.setIssueId(couponIssue.getId());
                issueRetail.setCouponId(Long.valueOf(couponInfo1.getId()));
                issueRetail.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
                i=  couponIssueRelationService.insertCouponIssueRelation(issueRetail);
            }
        }
        }
        // 更新优惠券信息中的最低使用金额
        List<CouponInfo> list = couponIssue.getCouponList();
        if(list!=null && list.size()>0){
            for (CouponInfo couponInfo1 : list){
                CouponInfo couponInfo=couponInfoService.selectCouponInfoById(couponInfo1.getId());
                couponInfo.setMinUseCouponAmt(couponInfo1.getMinUseCouponAmt());
                i=couponInfoService.updateCouponInfo(couponInfo);
            }
        }

        return AjaxResult.success(ordNo);
    }

    /**
     * 修改优惠劵发放方式
     */
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody CouponIssue couponIssue)
    {
        int i=0;
        if(couponIssue!=null &&couponIssue.getShowIdList()!=null && couponIssue.getShowIdList().size()>0){
            String verification= StringUtils.join( couponIssue.getShowIdList(),",");
            couponIssue.setShowId(verification);
        }


        if(couponIssue!=null && couponIssue.getMemberLevelList()!=null && couponIssue.getMemberLevelList().size()>0){
            couponIssue.setMemberLevel(StringUtils.join( couponIssue.getMemberLevelList(),","));
        }

        if(couponIssue!=null && couponIssue.getPaymentMethodList()!=null && couponIssue.getPaymentMethodList().size()>0){
            couponIssue.setPaymentMethod(StringUtils.join( couponIssue.getPaymentMethodList(),","));
        }
        if(couponIssue!=null && couponIssue.getMemberTagsList()!=null && couponIssue.getMemberTagsList().size()>0){
            couponIssue.setMemberTags(StringUtils.join( couponIssue.getMemberTagsList(),","));
        }


        LoginUser currentUser = SecurityUtils.getLoginUser();

        couponIssue.setStatus("1");
        List<CouponInfo> list = couponIssue.getCouponList();
        couponIssue.setUpdateBy(String.valueOf(currentUser.getUser().getUserId()));
        i=couponIssueService.updateCouponIssue(couponIssue);
        //删除所有的已绑定的优惠劵
        CouponIssueRelation detail = new CouponIssueRelation();
        detail.setIssueId(couponIssue.getId());
        couponIssueRelationService.deleteCouponIssueRelation(detail);
        iIssueOilService.deleteIssueOilRelationsByIssueId(couponIssue.getId());
        iIssueStationService.deleteIssueStationRelationsByIssueId(couponIssue.getId());
        if(couponIssue!=null && couponIssue.getOilIdLists()!=null && couponIssue.getOilIdLists().size()>0){
            for(SysOil sysOil:couponIssue.getOilIdLists()){
                IssueOilRelations issueOilRelations=new IssueOilRelations();
                issueOilRelations.setIssueId(couponIssue.getId());
                issueOilRelations.setOilId(sysOil.getOilId());
                iIssueOilService.insertIssueOilRelations(issueOilRelations);
            }
        }
        if (couponIssue != null && couponIssue.getCouponNotOils() != null && couponIssue.getCouponNotOils().size() > 0) {
            for (CouponNotOil sysOil : couponIssue.getCouponNotOils()) {
                IssueOilRelations issueOilRelations = new IssueOilRelations();
                issueOilRelations.setIssueId(couponIssue.getId());
                issueOilRelations.setOilId(sysOil.getCommodityId());
                iIssueOilService.insertIssueNotOilRelations(issueOilRelations);
            }
        }
        if(couponIssue!=null && couponIssue.getStationLists()!=null && couponIssue.getStationLists().size()>0){
            for(SysStationUser sysStationUser:couponIssue.getStationLists()){
                IssueStationRelations issueStationRelations=new IssueStationRelations();
                issueStationRelations.setIssueId(couponIssue.getId());
                issueStationRelations.setStationId(sysStationUser.getStationId());
                iIssueStationService.insertIssueStationRelations(issueStationRelations);
            }
        }
//        if(list!=null && list.size()>0){
//            for (CouponInfo couponInfo1 : list){
//                CouponIssueRelation issueRetail = new CouponIssueRelation();
//                CouponInfo couponInfo=couponInfoService.selectCouponInfoById(couponInfo1.getId());
//                couponInfo.setEditFlag("1");
//                couponInfoService.updateCouponInfo(couponInfo);
//                issueRetail.setIssueId(couponIssue.getId());
//                issueRetail.setCouponId(Long.valueOf(couponInfo1.getId()));
//                issueRetail.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
//                couponIssueRelationService.insertCouponIssueRelation(issueRetail);
//            }
//        }
        if("ladder".equals(couponIssue.getThresholdType())) {
            if (couponIssue.getThresholdLadder() != null && couponIssue.getThresholdLadder().size() > 0) {
                for (Threshold threshold : couponIssue.getThresholdLadder()) {
                    if (threshold.getCouponList() != null && threshold.getCouponList().size() > 0) {
                        for (String id : threshold.getCouponList()) {
                            CouponIssueRelation issueRetail = new CouponIssueRelation();
                            CouponInfo couponInfo = couponInfoService.selectCouponInfoById(Integer.parseInt(id));
                            couponInfo.setEditFlag("1");
                            i = couponInfoService.updateCouponInfo(couponInfo);
                            issueRetail.setIssueId(couponIssue.getId());
                            issueRetail.setCouponId(Long.valueOf(id));
                            issueRetail.setThresholdAmt(threshold.getValue());
                            issueRetail.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
                            i = couponIssueRelationService.insertCouponIssueRelation(issueRetail);
                        }
                    }
                }
            }
        }else {
            List<CouponInfo> list1 = couponIssue.getCouponList();
            if(list1!=null && list1.size()>0){
                for (CouponInfo couponInfo1 : list1){
                    CouponIssueRelation issueRetail = new CouponIssueRelation();
                    CouponInfo couponInfo=couponInfoService.selectCouponInfoById(couponInfo1.getId());
                    couponInfo.setEditFlag("1");
                    i=couponInfoService.updateCouponInfo(couponInfo);
                    issueRetail.setIssueId(couponIssue.getId());
                    issueRetail.setCouponId(Long.valueOf(couponInfo1.getId()));
                    issueRetail.setCreateBy(String.valueOf(currentUser.getUser().getUserId()));
                    i=  couponIssueRelationService.insertCouponIssueRelation(issueRetail);
                }
            }
        }
        List<CouponInfo> list1 = couponIssue.getCouponList();
        if(list1!=null && list1.size()>0){
            for (CouponInfo couponInfo1 : list1){
                CouponInfo couponInfo=couponInfoService.selectCouponInfoById(couponInfo1.getId());
                couponInfo.setMinUseCouponAmt(couponInfo1.getMinUseCouponAmt());
                i=couponInfoService.updateCouponInfo(couponInfo);
            }
        }
        return toAjax(i);
    }

    @PutMapping("/updateCouponIssueStatus")
    @Transactional
    public AjaxResult updateCouponIssueStatus(@RequestBody CouponIssue couponIssue)
    {
        int i=0;
        if(couponIssue!=null &&couponIssue.getShowIdList()!=null && couponIssue.getShowIdList().size()>0){
            String verification= StringUtils.join( couponIssue.getShowIdList(),",");
            couponIssue.setShowId(verification);
        }

        LoginUser currentUser = SecurityUtils.getLoginUser();
        couponIssue.setUpdateBy(String.valueOf(currentUser.getUser().getUserId()));
        i=couponIssueService.updateCouponIssue(couponIssue);
        return toAjax(i);
    }
    /**
     * 删除优惠劵发放方式
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(couponIssueService.deleteCouponIssueByIds(ids));
    }
}
